<?php


$vendor_name=$_POST['vendor_name'];
$invoice_number=$_POST['invoice_number'];
$vendor_number=$_POST['vendor_number'];
$quantity_received =$_POST['quantity_received'];
$amount_curr=$_POST['amount_curr'];
$po_num= $_POST['po_num'];
$account=$_POST['account'];
$fund=$_POST['fund'];
$project_activity=$_POST['project_activity'];
$donor=$_POST['donor'];
$date_received1= date("Y-F-d");
$date_received2= date("Y-F-d");

$t='<?xml version="1.0" standalone="yes"?><treaty></treaty>';
$treaty_xml = new SimpleXMLElement($t);
$treaty_xml->addChild("vendor_name",htmlspecialchars($vendor_name));
$treaty_xml->addChild("invoice_number",htmlspecialchars($invoice_number));
$treaty_xml->addChild("quantity_received",htmlspecialchars($quantity_received));
$treaty_xml->addChild("vendor_number",htmlspecialchars($vendor_number));
$treaty_xml->addChild("amount_curr",htmlspecialchars($amount_curr));
$treaty_xml->addChild("po_num",htmlspecialchars($po_num));
$treaty_xml->addChild("account",htmlspecialchars($account));
$treaty_xml->addChild("fund",htmlspecialchars($fund));
$treaty_xml->addChild("project_activity",htmlspecialchars($project_activity));
$treaty_xml->addChild("donor",htmlspecialchars($donor));
$treaty_xml->addChild("date_received1",htmlspecialchars($date_received1));
$treaty_xml->addChild("date_received2",htmlspecialchars($date_received2));


$fl = $treaty_xml->asXML();


$xsltFile = "accounts_payable.xml";
$sourceTemplate = "accounts_payable.docx";
$outputDocument = "downloaded.docx";

//Load the xml data and xslt and perform the transformation.
$xmlDocument = new DOMDocument();
//$xmlDocument->load($xmlDataFile);
$xmlDocument->loadXML($fl);

$xsltDocument = new DOMDocument();
$xsltDocument->load($xsltFile);

$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xsltDocument);

//After the transformation, $newContentNew contains 
//the XML data in the Open XML Wordprocessing format.
$newContent =  $xsltProcessor->transformToXML($xmlDocument);

//Copy the Word 2007 template document to the output file.
if (copy($sourceTemplate, $outputDocument)) {

  //Open XML files are packaged following the Open Packaging 
  //Conventions and can be treated as zip files when 
  //accessing their content.
  $zipArchive = new ZipArchive();
  $zipArchive->open($outputDocument);
  
  //Replace the content with the new content created above.
  //In the Open XML Wordprocessing format content is stored
  //in the document.xml file located in the word directory.
  $zipArchive->addFromString("word/document.xml", $newContent);
  $zipArchive->close();
  
  header("content-type:application/octet-stream");
  header('Content-Disposition: attachment; filename="Accounts Payable Downloaded.docx"');
  echo file_get_contents($outputDocument);
  //echo "Processing Complete";
}
 
?>